package online.sixfish.question.controller;

import online.sixfish.common.api.ApiResult;
import online.sixfish.common.pojo.Message;
import online.sixfish.common.pojo.State;
import online.sixfish.judger.pojo.JudgeProgramReq;
import online.sixfish.judger.pojo.TestPack;
import online.sixfish.judger.pojo.TestResult;
import online.sixfish.question.model.Type;
import online.sixfish.question.service.JudgeService;
import lombok.extern.slf4j.Slf4j;
import online.sixfish.question.service.impl.JudgeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 凯威
 */
@CrossOrigin
@RestController
@Slf4j
public class JudgeController {


    @Autowired
    public JudgeServiceImpl judgeService;


    @PostMapping("/judge")
    public Object judgeAll(@RequestBody TestPack testPack) {

        TestResult testResult = null;
        boolean setting = false;

        if (testPack.getType().equals(Type.MATCH)) {
//                判题
            testResult = judgeService.matchJudge(testPack);
//                设置
            setting = judgeService.settingForMatch(testResult, testPack);
            log.info("竞赛提交");
        } else if (testPack.getType().equals(Type.NORMAL)) {
//            普通提交走这里
            log.info("普通提交");
            testResult = judgeService.normalJudge(testPack);
            setting = judgeService.setting(testResult, testPack);
            setting = judgeService.mustSetting(testResult, testPack);
        } else {
            return new Message(State.FAILURE, null, "提交失败,没有这个提交选项");
        }


        return new Message(!setting ? State.FAILURE : State.SUCCESS, testResult, !setting ? "提交失败,请联系管理员!" : "提交成功!");
    }

    @PostMapping("/judge/one")
    public Object judgeOne(@RequestBody TestPack testPack) {
        TestResult testResult;
        boolean setting;
        if (testPack.getType() == null || testPack.getType().equals(Type.NORMAL)) {
            log.info("普通提交--->");
            testResult = judgeService.normalJudge(testPack);
            log.info("提交结果：{}", testResult);
            setting = judgeService.setting(testResult, testPack);
            setting = judgeService.mustSetting(testResult, testPack);
            return ApiResult.success(testResult);
        } else {
            return ApiResult.failed();
        }
    }

    @PostMapping("/judge/program")
    @PreAuthorize("hasRole('user')")
    public ApiResult judgeQueProgram(@RequestBody JudgeProgramReq judgeProgramReq) {
        boolean setting;
        log.info("编程题判题提交--->");
        TestResult result = judgeService.judgeProgram(judgeProgramReq);
        log.info("提交结果：{}", result);
//        setting = judgeService.setting(testResult, testPack);
//        setting = judgeService.mustSetting(testResult, testPack);
        return ApiResult.success(result);

    }


}
